package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.CustomVisit;
import com.ruoyi.system.service.ICustomVisitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * customVisitController
 *
 * @author ruoyi
 * @date 2022-06-07
 */
@RestController
@RequestMapping("/system/visit")
public class CustomVisitController extends BaseController
{
    @Autowired
    private ICustomVisitService customVisitService;

    /**
     * 查询customVisit列表
     */
    @PreAuthorize("@ss.hasPermi('system:visit:list')")
    @GetMapping("/list")
    public TableDataInfo list(CustomVisit customVisit)
    {
        // 判断当前登录用户角色
        if (getLoginUser().getUser().getRoleId() != 1) {
            customVisit.setUserId(getUserId());
        }
        startPage();
        List<CustomVisit> list = customVisitService.selectCustomVisitList(customVisit);
        return getDataTable(list);
    }

    /**
     * 导出customVisit列表
     */
    @PreAuthorize("@ss.hasPermi('system:visit:export')")
    @Log(title = "customVisit", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomVisit customVisit)
    {
        List<CustomVisit> list = customVisitService.selectCustomVisitList(customVisit);
        ExcelUtil<CustomVisit> util = new ExcelUtil<CustomVisit>(CustomVisit.class);
        util.exportExcel(response, list, "customVisit数据");
    }

    /**
     * 获取customVisit详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:visit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(customVisitService.selectCustomVisitById(id));
    }

    /**
     * 新增customVisit
     */
    @PreAuthorize("@ss.hasPermi('system:visit:add')")
    @Log(title = "customVisit", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomVisit customVisit)
    {
        customVisit.setUserId(getUserId());
        return toAjax(customVisitService.insertCustomVisit(customVisit));
    }

    /**
     * 修改customVisit
     */
    @PreAuthorize("@ss.hasPermi('system:visit:edit')")
    @Log(title = "customVisit", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomVisit customVisit)
    {
        return toAjax(customVisitService.updateCustomVisit(customVisit));
    }

    /**
     * 删除customVisit
     */
    @PreAuthorize("@ss.hasPermi('system:visit:remove')")
    @Log(title = "customVisit", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(customVisitService.deleteCustomVisitByIds(ids));
    }

    /**
     * 更新状态
     */
    @Log(title = "customVisit", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody CustomVisit customVisit)
    {
        return toAjax(customVisitService.updateCustomVisit(customVisit));
    }

}
